package com.qiyu.mall.user.provider.service;

import com.qiyu.mall.user.interfaces.dto.ShareHistroyDTO;
import com.qiyu.mall.user.interfaces.service.ShareHistroyService;
import com.qiyu.mall.user.provider.dao.ShareHistroyDao;
import com.qiyu.mall.user.provider.entity.ShareHistroyPO;
import com.qiyu.mall.user.provider.utils.BeanCopyUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author idea
 * @data 2020/2/12
 */
@Service
public class ShareHistroyServiceImpl implements ShareHistroyService {

    @Resource
    private ShareHistroyDao shareHistroyDao;

    /**
     * 将之前同类的更新为无效，然后记录新的一条信息
     * @param shareHistroyDTO
     */
    @Override
    public void insertOne(ShareHistroyDTO shareHistroyDTO) {
        Map<String,Object> paramMap=new HashMap<>(2);
        paramMap.put("product_id",shareHistroyDTO.getProductId());
        paramMap.put("account_id",shareHistroyDTO.getAccountId());
        paramMap.put("ca_id",shareHistroyDTO.getCaId());
        List<ShareHistroyPO> shareHistroyPOS = shareHistroyDao.selectByMap(paramMap);
        if(!CollectionUtils.isEmpty(shareHistroyPOS)){
            shareHistroyDao.updateShareHistory(shareHistroyDTO.getCaId(),shareHistroyDTO.getProductId(),shareHistroyDTO.getAccountId());
        }
        ShareHistroyPO shareHistroyPO=new ShareHistroyPO();
        BeanUtils.copyProperties(shareHistroyDTO,shareHistroyPO);
        shareHistroyDao.insert(shareHistroyPO);
    }



    @Override
    public  List<ShareHistroyDTO> selectByAccountId(Integer accountId) {
        Map<String,Object> param=new HashMap<>(1);
        param.put("account_id",accountId);
        param.put("valid_status",0);
        List<ShareHistroyPO> shareHistroyPOList=shareHistroyDao.selectByMap(param);
        List<ShareHistroyDTO> shareHistroyDTOList= BeanCopyUtil.copyList(shareHistroyPOList,ShareHistroyDTO.class);
        return shareHistroyDTOList;
    }


    @Override
    public void batchUpdateValidStatus(Integer accountId, Integer productId) {
        shareHistroyDao.updateOldShareHistory(productId,accountId);
    }
}
